package com.tojabo.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import com.tojabo.piano.PianoPlayThread;

public class SystemUtil {

	public static List<String> soundNameList = new ArrayList<String>();
	
	public static void play_Action(int index){
		String filename=getSoundNameByIndex(index);
		new PianoPlayThread(filename).start();
	}
	
	public static void showInCenter(JFrame frame) {
		// 显示位置
		int HEIGHT = frame.getHeight();
		int WIDTH = frame.getWidth();
		Toolkit kit = Toolkit.getDefaultToolkit();// 设置顶层容器框架为居中
		Dimension screenSize = kit.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		
		if(HEIGHT>height){
			height = HEIGHT;
		}
		if(WIDTH>width){
			width=WIDTH;
		}
		
		int x = (width - WIDTH) / 2;
		int y = (height - HEIGHT) / 2;
		frame.setLocation(x, y);
	}
	
	public static ImageIcon createImageIcon(String filename) {
		java.net.URL imgURL = SystemUtil.class.getClassLoader().getResource("com/tojabo/res/" + filename);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + filename);
			return null;
		}
	}
	
	public static InputStream getInputStream(String filename){
		InputStream in = SystemUtil.class.getClassLoader().getResourceAsStream("com/tojabo/res/" + filename);
			
		if (in != null) {
			return in;
		} else {
			System.err.println("Couldn't find file: " + filename);
			return null;
		}
	}
	
	public static String getSoundNameByIndex(int index){
		
		if(soundNameList.size()==0){
			String sounds = "c4.ogg,d4.ogg,e4.ogg,f4.ogg,g4.ogg,a4.ogg,b4.ogg,"+
							  "c5.ogg,d5.ogg,e5.ogg,f5.ogg,g5.ogg,a5.ogg,b5.ogg,"+
							  "c6.ogg,d6.ogg,e6.ogg,f6.ogg,g6.ogg,a6.ogg,b6.ogg";
			String[] soundArr = sounds.split(",");
			for(String sound : soundArr){
				soundNameList.add(sound);
			}
		}
		
		return soundNameList.get(index);
	}
	
}
